package com.lzy.liujing.restaurant.controller.sysController;

import com.lzy.liujing.restaurant.entity.*;
import com.lzy.liujing.restaurant.service.DeskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/desk")
public class DeskController {
    @Autowired
    private DeskService deskService;

    @GetMapping("/current/{current}/size/{size}")
    public Result getAllPage(@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<Desk> brandPageItem = deskService.queryPage(current, size);

        Result result = new Result(brandPageItem, "查询成功");
        return result;

    }

    @GetMapping("/current1/{current}/size1/{size}")
    public Result getAllPage1(@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<Desk> brandPageItem = deskService.queryPage(current, size);
        Result result = new Result(brandPageItem, "查询成功");
        return result;

    }

    @PostMapping("/current1/{current}/size1/{size}")
    public Result getCondPage1(@PathVariable Integer current,
                              @PathVariable Integer size,
                              @RequestBody Desk query) {
        //执行查询
        PageItem<Desk> item = deskService.queryConditionPage(query,current,size);

        Result result = new Result(item, "查询成功");
        return result;

    }

    @PostMapping("/current/{current}/size/{size}")
    public Result getCondPage(@PathVariable Integer current,
                              @PathVariable Integer size,
                              @RequestBody Desk query) {
        //执行查询
        PageItem<Desk> item = deskService.queryConditionPage(query,current,size);
        Result result = new Result(item, "查询成功");
        return result;

    }

    @DeleteMapping("/desks/{id}")
    public Result delete(@PathVariable Integer id, HttpServletRequest request) {
        Integer accountId = (Integer) request.getAttribute("accountId");
        boolean flag = deskService.delete(id);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }

    @GetMapping("/desk/{id}")
    public Result getOne(@PathVariable Integer id) {
        Desk byId = deskService.getById(id);
        if (byId == null) {
            return Result.fail(404, "查询不到该数据");
        }
        Result result = new Result(byId, "查询成功");
        return result;
    }

    @DeleteMapping("/desk/{ids}")
    public Result deleteMultiRec(@PathVariable int[] ids) {
        boolean flag = deskService.deleteSome(ids);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }
    @PostMapping("/desk")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody Desk brand) {

        System.out.println(brand);
        boolean insertBrand = deskService.insert(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @RequestMapping("/desk/update")
    // ResponseBody 将json字符串 转换为java 列表
    public Result updateRed(@RequestBody Desk brand) {

        boolean updateBrand = deskService.update(brand);
        if(updateBrand){
            return new Result(brand, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }

    @RequestMapping("/desk/updateStatus")
    public Result updateCode(@RequestBody Desk OrderUpdate) {
        boolean updateCode = deskService.updataCode(OrderUpdate.getDeskCode(), OrderUpdate.getIdleStatus());
        if(updateCode){
            return new Result(null, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }

    }
}
